<?php namespace Admin\Controller;

/*文章管理控制器*/
class ArticleController extends CommonController
{
	protected $db;
	public function __init()
	{
		$this->db= new \Common\Model\Article;
	}
	/*首页*/
	public function index()
	{
		/*总计数*/
		$count = $this->db->where("is_recycle",0)->count();
		/*分页*/
		$page = Page::row(2)->make($count);
		/*获取首页所有的数据*/
		$oldData = $this->db
					->join("category","category_cid","=","cid")
					->field("aid,title,cid,cname")
					->where("is_recycle",0)
					->limit(Page::limit())
					->get();
		//	p($oldData);
		

		/*分离变量*/
		View::with("page",$page);
		View::with("oldData",$oldData);
		
		View::make();
	}
	/*文章添加*/
	public function addArticle()
	{
		/*判断是否是post提交，自动验证后判断添加是否成功*/
		if(IS_POST)
		{
			if(!$this->db->store())
			{
				/*添加不成功*/
				View::error($this->db->getError());
			}else{
			/*添加成功*/
				View::success("添加成功",U('index'));
			
			}
		
		}
		
		/*处理所属 分类*/
		$cateData = Db::table("category")->get();
		/*变树状图结构*/
		$cateData = Data::tree($cateData,"cname");
		
//		var_dump($cateData);
		/*分离变量*/
		View::with("cateData",$cateData);
		
		/*处理标签*///tag表
		$tagData = Db::table("tag")->get();
		/*分离变量*/
		View::with("tagData",$tagData);
		
		View::make();
	}
	
	/*编辑文章*/
	public function edit()
	{
		/*判断是否是post提交*/
		if(IS_POST)
		{
			/*获取要编辑的id*/
				$aid = Q("post.aid");
			if(!$this->db->edit($aid))
			{
				/*如果编辑失败*/
				View::error($this->db->getError());
			}else{
				/*如果编辑成功*/
				View::success("编辑成功",U("index"));
			}
		}
		
		/*处理所属 分类*/
		$cateData = Db::table("category")->get();
		/*变树状图结构*/
		$cateData = Data::tree($cateData,"cname");
		
//		var_dump($cateData);
		/*分离变量*/
		View::with("cateData",$cateData);
		
		/*处理标签*///tag表
		$tagData = Db::table("tag")->get();
		/*分离变量*/
		View::with("tagData",$tagData);
		/*获取旧数据*/
		$aid = Q("get.aid",0,'intval');
		
		/*关联article_data文章数据表*/
		$oldData = $this->db
					->join("article_data","aid","=","article_aid")
					->where('aid',$aid)->first();
//		var_dump($oldData);
		
		/*分离变量*/
		View::with("oldData",$oldData);
		
		//文章标签中间表获得当前文章所对应的标签id
		//获取单一字段，获得是一个数组，多个时候使用
		$tids = Db::table("article_tag")->where("article_aid","=",$aid)->lists("tag_tid");
//		var_dump($tids);
		/*分离变量*/
		View::with("tids",$tids);
		View::make();
	}
	/*删除文章*/
	public function del()
	{
		/*获取要删除的序号aid*/
		$aid = Q("get.aid",0,'intval');
		p($aid);
		//将要删除的aid写入回收站
		$count = $this->db->where('aid',$aid)->Update(array("is_recycle"=>1));
		$this->success('删除成功',U('index'));
	}
	//回收站
	public function recycle()
	{
		/*总计数*/
		$count = $this->db->where("is_recycle",1)->count();
		/*分页*/
		$page = Page::row(2)->make($count);
		/*获取首页所有的数据*/
		$oldData = $this->db
					->join("category","category_cid","=","cid")
					->field("aid,title,cid,cname")
					->where("is_recycle",1)
					->limit(Page::limit())
					->get();
		//	p($oldData);
		

		/*分离变量*/
		View::with("page",$page);
		View::with("oldData",$oldData);

		
		View::make();
	}
	
	//还原
	public function restore()
	{
		//先获取要还原的序号aid
		$aid = Q("get.aid",0,'intval');
		//修改回收站的1
		$this->db->where('aid',$aid)->Update(array('is_recycle'=>0));
		//执行还原
		$this->success("还原成功",U('index'));
	}
	
	//彻底删除
	public function delete()
	{
		//先获得要删除的序号aid
		$aid =Q("get.aid",0,'intval');
		//1.删除article
		$this->db->where('aid',$aid)->delete();
		//2.删除article_tag
		//实例化
		$arctagModel = new \Common\Model\ArticleTag;
		$arctagModel->where("article_aid",$aid)->delete();
		//3.删除article_data
		$articleDataModel = new \Common\Model\ArticleData;
		$articleDataModel->where('article_aid',$aid)->delete();
		//执行删除
		$this->success('彻底删除成功',U('recycle'));
	}
	
	
	
	
	
	
	
	
	
	
}

























